package com.bringway.p2p.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bringway.framework.domain.SysBizAtt;
import com.bringway.global.dao.SysAttechmentDao;
import com.bringway.global.util.StringUtils;
import com.bringway.p2p.dao.P2pHplanDao;
import com.bringway.p2p.service.P2pHplanService;

import net.sf.json.JSONObject;

/**
 * @类名 p2pHplanServiceImpl
 * @功能 TODO H计划Service层实现类
 * @作者 js
 * @日期 2017年4月18日 上午10:55:18
 * 
 */
@Service
@Transactional
public class P2pHplanServiceImpl implements P2pHplanService {
	@Autowired
	private P2pHplanDao p2pHplanDao;
	@Autowired
	private SysAttechmentDao sysAttechmentDao;

	private Logger logger = Logger.getLogger(P2pHplanServiceImpl.class);

	@Override
	public List<Map<String, Object>> listXmSq(JSONObject params) {
		return p2pHplanDao.listXmSq(params);
	}

	@Override
	public int getXmSqCount(JSONObject params) {
		return p2pHplanDao.getXmSqCount(params);
	}

	@Override
	public List<Map<String, Object>> listXmKu(JSONObject params) {
		return p2pHplanDao.listXmKu(params);
	}

	@Override
	public int getXmKuCount(JSONObject params) {
		return p2pHplanDao.getXmKuCount(params);
	}

	@Override
	@Transactional("p2p")
	public synchronized JSONObject saveXmSq(JSONObject params) {
		JSONObject result = new JSONObject();
		result.put("success", true);
		// 判断所选项目是否可用并至少两个项目
		String gridIds[] = params.optString("gridId").split(",");
		if (gridIds.length < 2) {
			result.put("isSuc", false);
			result.put("msg", "请至少选择两个项目");
			return result;
		}
		for (String gridId : gridIds) {
			boolean isKyXmKu = p2pHplanDao.isKyXmKu(gridId);
			if (!isKyXmKu) {
				result.put("isSuc", false);
				result.put("msg", "所选项目中不可用项目,请重新选择");
				return result;
			}
		}
		// 保存申请
		String xmId = p2pHplanDao.saveXmSq(params);
		if (StringUtils.isNotEmpty(xmId)) {
			for (String kuId : gridIds) {
				// 保存关联表
				int count = p2pHplanDao.saveXmAndKu(xmId, kuId);
				if (count > 0) {
					// 标记已用
					int isUsed = p2pHplanDao.updateKuIsUsed(kuId, "01");
					if (isUsed > 0) {
						result.put("isSuc", true);
						result.put("msg", "保存申请成功");
					} else {
						throw new RuntimeException();// 手动抛出异常
					}
				} else {
					throw new RuntimeException();// 手动抛出异常
				}
			}
		} else {
			throw new RuntimeException();// 手动抛出异常
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> listKuByXmId(String xmId) {
		return p2pHplanDao.listKuByXmId(xmId);
	}

	@Override
	public int getKuByXmIdCount(String xmId) {
		return p2pHplanDao.getKuByXmIdCount(xmId);
	}

	@Override
	@Transactional("p2p")
	public synchronized JSONObject saveXmSh(JSONObject params) {
		JSONObject result = new JSONObject();
		String xmSqId = params.optString("xmSqId");
		int isGotoSh = p2pHplanDao.isGotoSh(xmSqId);
		if (isGotoSh <= 0) {
			result.put("isSuc", false);
			result.put("msg", "项目已经审核过,请刷新重新选择");
			return result;
		}
		String shzt = params.optString("shzt");
		if ("01".equals(shzt)) {// 通过
			params.put("xmzt", "4");
			int updateXmSqInfo = p2pHplanDao.updateXmSqInfo(params);
			if (updateXmSqInfo > 0) {
				Map<String, Object> xmSqMap = p2pHplanDao.getXmSqMapById(xmSqId);
				String xmId = p2pHplanDao.saveXmSh(xmSqMap);
				if (StringUtils.isNotEmpty(xmId)) {// 插入项目表成功
					// 更新t_zhfw_h_plan_xm_ku表
					int updateXmKuByXmSqId = p2pHplanDao.updateXmKuByXmSqId(xmSqId, xmId);
					if (updateXmKuByXmSqId > 0) {
						result.put("isSuc", true);
						result.put("msg", "审核成功");
					} else {
						logger.error("saveXmSh 发标审核--更新关联表失败");
						throw new RuntimeException();// 手动抛出异常
					}
				} else {
					logger.error("saveXmSh 发标审核--插入项目表失败");
					throw new RuntimeException();// 手动抛出异常
				}
			} else {
				logger.error("saveXmSh 发标审核--更新申请表失败");
				throw new RuntimeException();// 手动抛出异常
			}

		} else if ("02".equals(shzt)) {// 不通过
			params.put("xmzt", "2");
			int updateXmSqInfo = p2pHplanDao.updateXmSqInfo(params);
			if (updateXmSqInfo > 0) {
				//释放项目库项目可用
				int updateKuByXmSqId=p2pHplanDao.updateKuByXmSqId(xmSqId);
				if (updateKuByXmSqId>0) {
					result.put("isSuc", true);
					result.put("msg", "审核成功");
				} else {
					logger.error("saveXmSh 发标审核--审核不通过,释放项目库中项目失败");
					throw new RuntimeException();// 手动抛出异常
				}
			} else {
				logger.error("saveXmSh 发标审核--审核不通过,更新申请表失败");
				throw new RuntimeException();// 手动抛出异常
			}
		} else {
			result.put("isSuc", false);
			result.put("msg", "审核状态不明,请重新审核");
		}
		return result;
	}

	@Override
	public List queryHplanList(JSONObject params) {
		return p2pHplanDao.queryHplanList(params);
	}

	@Override
	public int getHplanCount(JSONObject params) {
		return p2pHplanDao.getHplanCount(params);
	}

	@Override
	public int getQxDayByQxMonth(String qxMonth) {
		return p2pHplanDao.getQxDayByQxMonth(qxMonth);
	}

	@Override
	public String saveHplan(JSONObject params) {
		String bizId = p2pHplanDao.saveHplan(params);
		String atts = params.optString("atts");
		logger.info(atts);
		String[] att = atts.split(",");
		for (int i = 0; i < att.length; i++) {
			if ("".equals(att[i]))
				continue;
			SysBizAtt bizAtt = new SysBizAtt();
			bizAtt.setBizId(bizId);
			bizAtt.setAttId(Long.decode(att[i]));
			sysAttechmentDao.createBizAtt(bizAtt);
		}
		return bizId;
	}

	@Override
	@Transactional("p2p")
	public synchronized JSONObject saveShplan(JSONObject params) {
		JSONObject result = new JSONObject();
		String hid = params.optString("hid");
		int isGotoShplan = p2pHplanDao.isGotoShplan(hid);
		if (isGotoShplan <= 0) {
			result.put("isSuc", false);
			result.put("msg", "项目已经审核过,请刷新重新选择");
			return result;
		}
		String hstate = params.optString("hstate");
		if ("2".equals(hstate)) {// 通过
			// 跟新申请表信息
			int updateSqInfo = p2pHplanDao.updateSqInfo(params);
			if (updateSqInfo > 0) {
				// 查询申请表洗信息
				Map<String, Object> xmSqPlan = p2pHplanDao.querySq(hid);
				// 添加到审核库
				String bzid = p2pHplanDao.saveShplan(xmSqPlan);
				if (StringUtils.isNotEmpty(bzid)) {
					//更新附件
					sysAttechmentDao.delAttByBizId(hid);
					String atts = params.optString("atts");
					logger.info(atts);
					String[] att = atts.split(",");
					for (int i = 0; i < att.length; i++) {
						if ("".equals(att[i]))
							continue;
						SysBizAtt bizAtt = new SysBizAtt();
						bizAtt.setBizId(hid);
						bizAtt.setAttId(Long.decode(att[i]));
						sysAttechmentDao.createBizAtt(bizAtt);
					}
					result.put("isSuc", true);
					result.put("msg", "审核成功");
				}else {
					logger.error("saveShplan 发标审核--添加到项目库失败");
					throw new RuntimeException();// 手动抛出异常
				}
			} else {
				logger.error("saveShplan 发标审核--更新申请表失败");
				throw new RuntimeException();// 手动抛出异常
			}
		} else if ("3".equals(hstate)) {// 不通过
			// 跟新申请表信息
			int updateSqInfo = p2pHplanDao.updateSqInfo(params);
			if (updateSqInfo > 0) {
				result.put("isSuc", true);
				result.put("msg", "审核成功");
			} else {
				logger.error("saveShplan 发标审核--审核不通过,更新申请表失败");
				throw new RuntimeException();// 手动抛出异常
			}
		} else {
			result.put("isSuc", false);
			result.put("msg", "审核状态不明,请重新审核");
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> queryHzhjh(JSONObject params) {
	
		return p2pHplanDao.queryHzhjh(params);
	}

	@Override
	public int getHzhjhCount(JSONObject params) {
		
		return p2pHplanDao.getHzhjhCount(params);
	}
	@Override
	public String geHzhjhSum(JSONObject params) {
		return this.p2pHplanDao.geHzhjhSum(params);
	}
	
	@Override
	public List<Map<String, Object>> queryHtzjl(JSONObject params) {
		return p2pHplanDao.queryHtzjl(params);
	}

	@Override
	public int getHtzjlCount(JSONObject params) {
		return p2pHplanDao.getHtzjlCount(params);
	}
	@Override
	public String geHtzjlSum(JSONObject params) {
		return this.p2pHplanDao.geHtzjlSum(params);
	}
	@Override
	public List<Map<String, Object>> queryHlbtzjl(JSONObject params) {
		return p2pHplanDao.queryHlbtzjl(params);
	}
	
	@Override
	public int getHlbtzjlCount(JSONObject params) {
		return p2pHplanDao.getHlbtzjlCount(params);
	}
	@Override
	public String geHlbtzjlSum(JSONObject params) {
		return p2pHplanDao.geHlbtzjlSum(params);
	}
	@Override
	public List<Map<String, Object>> queryHsyffmx(JSONObject params) {
		return p2pHplanDao.queryHsyffmx(params);
	}
	@Override
	public int getHsyffmxCount(JSONObject params) {
		return p2pHplanDao.getHsyffmxCount(params);
	}
	@Override
	public List<Map<String, Object>> queryHhk(JSONObject params) {
		return p2pHplanDao.queryHhk(params);
	}
	
	@Override
	public int getHhkCount(JSONObject params) {
		return p2pHplanDao.getHhkCount(params);
	}

}
